package xworker.swt.xworker.attributeEditor.editors;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;
import org.xmeta.ActionContext;
import org.xmeta.Thing;
import org.xmeta.World;
import org.xmeta.util.ActionContainer;
import xworker.swt.design.Designer;
import xworker.swt.form.FormModifyListener;
import xworker.swt.xwidgets.SelectContent;
import xworker.swt.xworker.attributeEditor.AttributeEditor;

public class ContentSelectorEditor extends AttributeEditor {

    public ContentSelectorEditor(Thing formThing, Thing attribute, xworker.swt.form.GridData gridData, ActionContext actionContext) {
        super(formThing, attribute, gridData, actionContext);
    }

    @Override
    public Control createControl(Composite parent, FormModifyListener modifyListener, Listener defaultSelection) {
        int dataSelectCompositeStyle = SWT.NONE;
        Composite dataSelectComposite = new Composite(parent, dataSelectCompositeStyle);

        GridLayout dataSelectComGridLayout = new GridLayout();
        dataSelectComGridLayout.numColumns = 2;
        dataSelectComGridLayout.marginWidth= 0;
        dataSelectComGridLayout.marginHeight= 0;
        dataSelectComposite.setLayout(dataSelectComGridLayout);

        GridData dataSelectCompositeGridData = new GridData();
        dataSelectCompositeGridData.horizontalSpan = getColspan(xgridData.colspan);
        dataSelectCompositeGridData.verticalSpan = xgridData.rowspan;
        dataSelectComposite.setLayoutData(dataSelectCompositeGridData);

        int dataSelectTextStyle = SWT.SINGLE | SWT.LEFT;
        dataSelectTextStyle |= SWT.BORDER;
        Text dataSelectText = new Text(dataSelectComposite, dataSelectTextStyle);
        //dataSelectText.setFont(Resources.getFont("default"));
        Designer.attach(dataSelectText, path, context, true);

        GridData dataSelectTextGridData = new GridData(GridData.FILL_HORIZONTAL);
        dataSelectTextGridData.widthHint = _width * attribute.getInt("size", 25);
        dataSelectText.setLayoutData(dataSelectTextGridData);

        int selectDataButtonStyle = SWT.PUSH;
        Button selectDataButton = new Button(dataSelectComposite, selectDataButtonStyle);
        selectDataButton.setText("*");
        selectDataButton.addListener(SWT.Selection, event -> {
            ActionContext newContext = new ActionContext();
            newContext.put("shell", parent.getShell());
            newContext.put("parent", parent.getShell());
            newContext.put("text", dataSelectText);

            Thing thing = World.getInstance().getThing("xworker.swt.xworker.attributeEditor.prototypes.ContentSelectorShell");
            Shell shell = thing.doAction("create", newContext);

            String actionName = this.getParams().get("actionName");
            if(actionName == null){
                actionName = "getContents";
            }
            java.util.List<SelectContent> contents = attribute.doAction(actionName, this.context);
            ActionContainer contentSelector = newContext.getObject("contentSelector");
            if(contents != null){
                contentSelector.doAction("setContents", newContext, "contents", contents);
                contentSelector.doAction("setText", newContext, "text", "");
            }
            Button okButton = newContext.getObject("okButton");
            okButton.addListener(SWT.Selection, event2 -> {
                SelectContent content = contentSelector.doAction("getSelectedContent", newContext);
                if(content != null && content.value != null) {
                    dataSelectText.setText(content.value);
                }
                shell.dispose();
            });
            Button cancelButton = newContext.getObject("cancelButton");
            cancelButton.addListener(SWT.Selection, event1 -> {
                shell.dispose();
            });
            shell.setVisible(true);
        });

        if(modifyListener != null){
            dataSelectText.addModifyListener(modifyListener);
        }
        addDefaultSelection(dataSelectText, defaultSelection);
        context.getScope(0).put(inputName, dataSelectText);

        return dataSelectComposite;
    }

    //xworker.swt.xworker.attributeEditor.prototypes.ContentSelectorShell/@contentSelector/@actions/@selected
    public static void contentSelected(ActionContext actionContext){
        String value = actionContext.getObject("value");
        Text text = actionContext.getObject("text");
        Shell shell = actionContext.getObject("shell");
        if(value != null){
            text.setText(value);
        }

        shell.dispose();
    }
}
